package it.poste.il.compensatore.dao.impl;

import java.util.Properties;

import it.poste.il.compensatore.dao.OperationDao;
import it.poste.il.compensatore.entities.Operation;
import it.poste.il.compensatore.utilities.LoadQueriesSQL;

import javax.persistence.NoResultException;
import javax.persistence.Query;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * Implementation of OperationDao.
 */
public class OperationDaoImpl extends BaseEntityDaoImpl<Operation> implements OperationDao {
    public OperationDaoImpl() {
        super(Operation.class, "Operation");
    }

    LoadQueriesSQL loadQueriesSQL;
    
	public LoadQueriesSQL getLoadQueriesSQL() {
		return loadQueriesSQL;
	}

	public void setLoadQueriesSQL(LoadQueriesSQL loadQueriesSQL) {
		this.loadQueriesSQL = loadQueriesSQL;
	}

	/**
	 * Take the ObjHelper from operation.
	 * @param opt Entity Operation.
	 * @return String The ObjHelper of the operation.
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
	public String selectObjHelper(Operation opt){
		try {
			Properties p = loadQueriesSQL.getProperties();
			String querySQL = p.getProperty("selectOperationObjHelper");
			Query query = getEntityManager().createNativeQuery(querySQL, String.class).
					setParameter("id", opt.getId());
			return (String) query.getResultList().get(0);
		} catch (NoResultException e) {
		      return null;
		}
	}
	
}
